import time
import threading
import ttkbootstrap as ttk
from tkinter import StringVar, IntVar, messagebox
from module.public import check_game_status, find_pid_by_name
from module.sun import modify_sunlight, infinite_sunlight

class GameModifierApp:
    def __init__(self, root):
        self.root = root
        self.root.title("融合版修改器V1.0 QQ2843467603")
        self.root.geometry("800x400")  # Set the default window size
        self.status_text = StringVar()
        self.sun_value = IntVar()
        self.infinite_sunlight_active = False

        self.create_widgets()
        self.start_status_check()

    def create_widgets(self):
        self.status_label = ttk.Label(self.root, textvariable=self.status_text, font=("Helvetica", 16))
        self.status_label.pack(pady=10)

        # 创建一个Frame来包含输入框和按钮
        input_frame = ttk.Frame(self.root)
        input_frame.pack(pady=10)

        self.sun_entry = ttk.Entry(input_frame, textvariable=self.sun_value, font=("Helvetica", 14))
        self.sun_entry.grid(row=0, column=0, padx=5)

        self.modify_button = ttk.Button(input_frame, text="修改阳光", command=self.modify_sun)
        self.modify_button.grid(row=0, column=1, padx=5)#位置在哪 就是定位 比如说偶top：200 left:300这样，和前端grid布局一样，一行分成固定格数
#就是说自动排版，只用在意上下前后 对的
        self.infinite_button = ttk.Button(input_frame, text="一键无限阳光", command=self.toggle_infinite_sunlight)
        self.infinite_button.grid(row=0, column=2, padx=5)

        self.success_label = ttk.Label(self.root, text="", font=("Helvetica", 12))
        self.success_label.pack(pady=10)

        self.test_button = ttk.Button(self.root, text="使用完美存档", command=self.test_action)
        self.test_button.pack(pady=10)

        self.copyright_label = ttk.Label(self.root, text="© 版权所有By、慕容若兰, 2024. QQ 2843467603", font=("Helvetica", 10))
        self.copyright_label.pack(side="bottom", pady=10)

    def start_status_check(self):
        self.status_check_thread = threading.Thread(target=self.check_status_loop)
        self.status_check_thread.daemon = True
        self.status_check_thread.start()

    def check_status_loop(self):
        while True:
            try:
                game_ready = check_game_status()
                if game_ready:
                    self.status_text.set("已就绪")
                    if self.infinite_sunlight_active:
                        infinite_sunlight()
                else:
                    self.status_text.set("游戏未运行")
                time.sleep(1)
            except Exception as e:
                self.show_error(f"检查游戏状态时发生错误: {e}")

    def validate_input(self, *args):
        value = self.sun_value.get()
        if not isinstance(value, int):
            value = ''.join(filter(str.isdigit, str(value)))
            self.sun_value.set(int(value) if value else 0)
        if len(str(value)) > 5:
            self.show_error("输入无效，阳光值不能超过5位数")
            self.sun_value.set(int(str(value)[:5]))

    def modify_sun(self):
        try:
            sun_value = self.sun_value.get()
            if 0 <= sun_value <= 99999:
                modify_sunlight(sun_value)
                self.update_success_label(f"修改 阳光 {sun_value} 成功", "green")
            else:
                self.show_error("输入无效，必须是0到99999之间的数字")
        except Exception as e:
            self.show_error(f"修改阳光时发生错误: {e}")

    def toggle_infinite_sunlight(self):
        try:
            self.infinite_sunlight_active = not self.infinite_sunlight_active
            if self.infinite_sunlight_active:
                self.infinite_button.config(text="停止无限阳光")
                self.update_success_label("一键无限阳光开始", "green")
            else:
                self.infinite_button.config(text="一键无限阳光")
                self.update_success_label("一键无限阳光停止", "red")
        except Exception as e:
            self.show_error(f"切换无限阳光模式时发生错误: {e}")

    def test_action(self):
        print("测试按钮点击")

    def show_error(self, message):
        messagebox.showerror("错误", f"{message}\n请截图并反馈。")

    def update_success_label(self, action, color="black"):
        current_time = time.strftime("%H:%M:%S")
        self.success_label.config(text=f"{action}成功 {current_time}", foreground=color)

if __name__ == "__main__":
    root = ttk.Window(themename="darkly")
    app = GameModifierApp(root)
    root.mainloop()
